package cn.edu.suda.schoolBus.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.edu.suda.schoolBus.bean.AdminUser;
import cn.edu.suda.schoolBus.bean.AdminUserExample;
import cn.edu.suda.schoolBus.dao.AdminUserMapper;
import cn.edu.suda.schoolBus.service.AdminUserService;
import cn.edu.suda.schoolBus.util.ResultUtil;

@Service
public class AdminUserServiceImpl implements AdminUserService {

	@Autowired
	private AdminUserMapper adminUserDao;

	private AdminUserExample adminUserExample;

	@Override
	public int addAdminUser(AdminUser adminUser) {
		adminUser.setAuTime(new Date());
		return adminUserDao.insertSelective(adminUser);
	}

	@Override
	public Map<String, Object> getAdminUser(int page, int rows) {
		int start = (page - 1) * rows;
		adminUserExample = new AdminUserExample();
		adminUserExample.createCriteria().andAuIdIsNotNull();
		Map<String, Object> map = new HashMap<>();
		map.put("total", adminUserDao.countByExample(adminUserExample));
		map.put("rows", adminUserDao.selectByPage(start, rows));
		return map;
	}

	@Override
	public int deleteAdminUser(String ids) {
		adminUserExample = new AdminUserExample();
		adminUserExample.createCriteria().andAuIdIn(ResultUtil.getResult(ids));
		return adminUserDao.deleteByExample(adminUserExample);
	}

	@Override
	public int editAdminUser(AdminUser adminUser) {
		return adminUserDao.updateByPrimaryKeySelective(adminUser);
	}

	@Override
	public AdminUser getAdminUser(String username, String passwd) {
		adminUserExample = new AdminUserExample();
		adminUserExample.createCriteria().andAuAccountEqualTo(username).andAuPasswdEqualTo(passwd);
		List<AdminUser> adminUsers = adminUserDao.selectByExample(adminUserExample);
		AdminUser adminUser = null;
		if (adminUsers.size() == 1) {
			Iterator<AdminUser> aIterator = adminUsers.iterator();
			while (aIterator.hasNext()) {
				adminUser = aIterator.next();
			}
		}
		return adminUser;
	}

	@Override
	public int getAdminUser(String auAccount) {
		adminUserExample = new AdminUserExample();
		adminUserExample.createCriteria().andAuAccountEqualTo(auAccount);
		return adminUserDao.selectByExample(adminUserExample).size();
	}

}
